package com.qf.qfcenterservice.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.common.bean.R;
import com.qf.common.feigns.ProductApi;
import com.qf.common.model.QfProduct;
import com.qf.qfcenterservice.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class ProductFeignApi implements ProductApi {

    @Autowired
    private ProductService productService;

    @GetMapping("/api/product/list")
    @Override
    public R list(@RequestParam("tenantId") Long tenantId) {


        LambdaQueryWrapper<QfProduct> qw = Wrappers.lambdaQuery(QfProduct.class)
                .eq(QfProduct::getTenantId, tenantId);


        IPage<QfProduct> page = new Page<>(1,20);

        productService.page(page,qw);

        log.info("---products:{}",page.getRecords());

        return R.ok(page);
    }

    @Override
    public QfProduct findByProductKey(String productKey) {
        return productService.getOne(Wrappers.lambdaQuery(QfProduct.class).eq(QfProduct::getProductKey,productKey));
    }
}
